from ftllibs.items import *
import operator
import copy
import sys
import re

class Items():
    def __init__(self,parent):
        self.parent=parent
        self.ITEMS={}
        self.ItemIndex=[]
        for i in sys.modules.keys():
            m = re.match('ftllibs.items.i(\d+)',i)
            if m:
                self.ItemIndex.append(m.groups(0)[0])
        self.InitItems()
        self.CreateLists()

    def InitItems(self):
        for i in self.ItemIndex:
            cmd = "self.ITEMS['%s']=i%s.Item_%s()" % (i,i,i)
            exec cmd
#        self.ITEMS['3001']=i3001.Item_3001()

    def GetItem(self,itemid):
        return self.ITEMS[str(itemid)]
        
    def GetItemCopy(self,itemid):
        return copy.deepcopy(self.ITEMS[str(itemid)])

    def CreateLists(self):
        # sortedLists is for item attributes , like health , mana and so on
        self.SortedLists={}
        for ItemID in self.ItemIndex:
            for i in self.ITEMS[ItemID].Tags:
                if not self.SortedLists.has_key(i):
                    self.SortedLists[i]=[]
                    self.SortedLists[i].append(ItemID)
                else:
                    self.SortedLists[i].append(ItemID)
        for i in self.SortedLists:
            costs = {}
            #for var in collection:
            for j in self.SortedLists[i]:
                costs[j] = self.ITEMS[j].Total_Costs
            self.SortedLists[i] = []
            for k,l in sorted(costs.iteritems(), key=operator.itemgetter(1)):
                self.SortedLists[i].append(k)
        # Availability list speaks for itself
        self.AvailabilityLists={}
        self.AvailabilityLists['summonersrift']=[]
        self.AvailabilityLists['twistedtreeline']=[]
        self.AvailabilityLists['dominion']=[]
        for ItemID in self.ItemIndex:
            for i in self.ITEMS[ItemID].Availability:
                if i == "summonersrift" or i == "all":
                    self.AvailabilityLists['summonersrift'].append(ItemID)
                if i == "twistedtreeline" or i == "all":
                    self.AvailabilityLists['twistedtreeline'].append(ItemID)
                if i == "dominion" or i == "all":
                    self.AvailabilityLists['dominion'].append(ItemID)

    def GetAvailabilityList(self,string):
        return self.AvailabilityLists[string]

    def GetList(self,string):
        #attack_speed
        #doran
        #life_steal
        #spell_damage
        #viktor
        #armor
        #spell_block
        #damage
        #health_regen
        #mana
        #health
        #consumeable
        #movement
        #mana_regen
        #critical_strike
        #prospector
        #cooldown_reduction
        return self.SortedLists[string]
